<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Add_truck
 *
 * @author admin
 */
class edit_driver extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('driver_model');
        $this->load->model('catalog');
    }

    public function index() {
        $id = $this->input->get('id');
        $this->_set_form($id);
        $this->load->view('driver/edit_driver', $this->data);
    }

    private function _set_form($id) {
        $utility = new Config_utility();
        $driver = $this->driver_model->get_driver_by_id($id);
        $this->data['hid_id'] = form_hidden('id', $id);
        $this->data['cmb_prefix'] = form_dropdown('cmbPrefix', $this->catalog->list_prefix(), $driver[0]->prefix_id, 'class="form-control"');
        $this->data['txt_fname'] = form_input('txtFname', $driver[0]->first_name, 'class="form-control"');
        $this->data['txt_lname'] = form_input('txtLname', $driver[0]->last_name, 'class="form-control"');
        $this->data['txt_nname'] = form_input('txtNname', $driver[0]->nick_name, 'class="form-control"');
        $this->data['txt_citizenId'] = form_input('txtCitizenId', $driver[0]->citizen_id, 'class="form-control" maxlength="13"');
        $this->data['cmb_day'] = form_dropdown('cmbDay', $utility->list_day(), $utility->convert_date($driver[0]->birthday, $utility->DAY), 'class="form-control"');
        $this->data['cmb_month'] = form_dropdown('cmbMonth', $utility->list_month(), $utility->convert_date($driver[0]->birthday, $utility->MONTH), 'class="form-control"');
        $this->data['cmb_year'] = form_dropdown('cmbYear', $utility->list_year(), $utility->convert_date($driver[0]->birthday, $utility->YEAR), 'class="form-control"');
        $this->data['txt_address'] = form_textarea(array('name' => 'txtAddress', 'rows' => '3'), $driver[0]->address, 'class="form-control" style="resize: none;"');
        $this->data['cmb_province'] = form_dropdown('cmbProvince', $this->catalog->list_province(), $driver[0]->province_id, 'class="form-control"');
        $this->data['txt_postcode'] = form_input('txtPostCode', $driver[0]->postcode, 'class="form-control" maxlength="5"');
        $this->data['txt_telephone'] = form_input('txtTelephone', $driver[0]->telephone, 'class="form-control" maxlength="10"');
        $this->data['cmb_status'] = form_dropdown('cmbStatus', $this->catalog->list_driver_status(), $driver[0]->status_id, 'class="form-control"');

        $img = $this->driver_model->get_driver_img_by_id($id);
        if ($img != NULL) {
            $this->data['img_picture'] = '<img src="' . base_url() . 'uploads/drivers/' . $img[0]->file_name . '" style="width:355px" \>';
        } else {
            $this->data['img_picture'] = '<img src="' . base_url() . 'images/driver/no_image.jpg" style="width:355px"  \>';
        }
        $this->data['upload_pic'] = form_upload('uploadPic', '', 'style="margin-left:30px"');
        $this->_set_error();
    }

    private function _set_error() {
        $this->data['err_prefix'] = form_error('cmbPrefix', '<div class="error">', '</div>');
        $this->data['err_fname'] = form_error('txtFname', '<div class="error">', '</div>');
        $this->data['err_lname'] = form_error('txtLname', '<div class="error">', '</div>');
        $this->data['err_nname'] = form_error('txtNname', '<div class="error">', '</div>');
        $this->data['err_citizenId'] = form_error('txtCitizenId', '<div class="error">', '</div>');
        $this->data['err_day'] = form_error('cmbDay', '<div class="error">', '</div>');
        $this->data['err_month'] = form_error('cmbMonth', '<div class="error">', '</div>');
        $this->data['err_year'] = form_error('cmbYear', '<div class="error">', '</div>');
        $this->data['err_address'] = form_error('txtAddress', '<div class="error">', '</div>');
        $this->data['err_province'] = form_error('cmbProvince', '<div class="error">', '</div>');
        $this->data['err_postcode'] = form_error('txtPostCode', '<div class="error">', '</div>');
        $this->data['err_telephone'] = form_error('txtTelephone', '<div class="error">', '</div>');
        $this->data['err_status'] = form_error('cmbStatus', '<div class="error">', '</div>');
    }

    public function validate() {
        $id = $this->input->post('id');
        if (!empty($id)) {
            $this->form_validation->set_rules('cmbPrefix', 'Prefix', 'required');
            $this->form_validation->set_rules('txtFname', 'First Name', 'required');
            $this->form_validation->set_rules('txtLname', 'Last Name', 'required');
            $this->form_validation->set_rules('txtNname', 'Nick Name', 'required');
            $this->form_validation->set_rules('txtCitizenId', 'Citizen Id', 'required|numeric|exact_length[13]');
            $this->form_validation->set_rules('cmbDay', 'Day', 'required');
            $this->form_validation->set_rules('cmbMonth', 'Month', 'required');
            $this->form_validation->set_rules('cmbYear', 'Year', 'required');
            $this->form_validation->set_rules('txtAddress', 'Address', 'required');
            $this->form_validation->set_rules('cmbProvince', 'Province', 'required');
            $this->form_validation->set_rules('txtPostCode', 'Post Code', 'required|numeric|exact_length[5]');
            $this->form_validation->set_rules('txtTelephone', 'Telephone', 'required|numeric|exact_length[10]');
            $this->form_validation->set_rules('cmbStatus', 'Status', 'required');

            if ($this->form_validation->run() == FALSE) {
                $this->_set_form($id);
                $this->load->view('edit_driver', $this->data);
            } else {
                $prefix = $this->input->post('cmbPrefix');
                $fname = $this->input->post('txtFname');
                $lname = $this->input->post('txtLname');
                $nname = $this->input->post('txtNname');
                $citizenId = $this->input->post('txtCitizenId');

                $day = $this->input->post('cmbDay');
                $month = $this->input->post('cmbMonth');
                $year = $this->input->post('cmbYear');
                $birthday = $year . "/" . $month . "/" . $day;

                $address = $this->input->post('txtAddress');
                $province = $this->input->post('cmbProvince');
                $postcode = $this->input->post('txtPostCode');
                $telephone = $this->input->post('txtTelephone');
                $status = $this->input->post('cmbStatus');

                $config['upload_path'] = './uploads/drivers/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '2048';
                $config['overwrite'] = FALSE;
                $config['encrypt_name'] = TRUE;
                $this->load->library('upload', $config);
                if ($_FILES['uploadPic']['name'] != NULL) {
                    if (!$this->upload->do_upload('uploadPic')) {
                        $this->data['err_upload'] = $this->upload->display_errors('<div class="error">', '</div>');
                        $this->_set_form($id);
                        $this->load->view('driver/edit_driver', $this->data);
                    } else {
                        //To save
                        $upload_data = $this->upload->data();
                        $is_result = $this->driver_model->update_data($id, $prefix, $fname, $lname, $nname, $citizenId, $birthday, $address, $province, $postcode, $telephone, $status);
                        $img = $this->driver_model->get_driver_img_by_id($id);
                        if ($img != NULL) {
                            $this->driver_model->update_image($id, $upload_data['file_name']);
                        } else {
                            $this->driver_model->save_image($id, $upload_data['file_name']);
                        }

                        if ($is_result == TRUE) {
                            $utility = new Config_form();
                            echo $utility->form_alert_success('บันทึกเสร็จเรียบร้อย', 'driver/home');
                            exit();
                        } else {
                            $this->_set_form($id);
                            $this->load->view('driver/edit_driver', $this->data);
                        }
                    }
                } else {
                    $is_result = $this->driver_model->update_data($id, $prefix, $fname, $lname, $nname, $citizenId, $birthday, $address, $province, $postcode, $telephone, $status);
                    if ($is_result == TRUE) {
                        $utility = new Config_form();
                        echo $utility->form_alert_success('บันทึกเสร็จเรียบร้อย', 'driver/home');
                        exit();
                    } else {
                        $this->_set_form($id);
                        $this->load->view('driver/edit_driver', $this->data);
                    }
                }
            }
        } else {
            redirect('error');
        }
    }

}
